package cn.edu.baiyunu.myapplication.ui.screens.add

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import cn.edu.baiyunu.myapplication.ui.components.GradientBackground
import cn.edu.baiyunu.myapplication.ui.screens.add.viewmodel.AddEditTransactionViewModel

/**
 * 简化版的添加交易屏幕
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun SimpleAddTransactionScreen(
    onTransactionAdded: () -> Unit = {},
    onCancel: () -> Unit = {},
    viewModel: AddEditTransactionViewModel = hiltViewModel()
) {
    val uiState by viewModel.uiState.collectAsStateWithLifecycle()
    
    // 交易类型选择
    var expenseSelected by remember { mutableStateOf(true) }
    
    // 监控保存状态
    LaunchedEffect(uiState.isSaved) {
        if (uiState.isSaved) {
            onTransactionAdded()
        }
    }
    
    // 设置交易类型
    LaunchedEffect(expenseSelected) {
        viewModel.setTransactionType(if (expenseSelected) "expense" else "income")
    }
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text(if (uiState.isEditMode) "编辑交易" else "添加交易") },
                navigationIcon = {
                    IconButton(onClick = onCancel) {
                        Icon(
                            imageVector = Icons.AutoMirrored.Filled.ArrowBack,
                            contentDescription = "返回"
                        )
                    }
                }
            )
        }
    ) { innerPadding ->
        // 背景渐变
        GradientBackground {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .padding(innerPadding)
                    .verticalScroll(rememberScrollState())
                    .padding(16.dp),
                verticalArrangement = Arrangement.spacedBy(16.dp)
            ) {
                // 交易类型选择
                Row(
                    modifier = Modifier.fillMaxWidth(),
                    horizontalArrangement = Arrangement.SpaceEvenly
                ) {
                    Button(
                        onClick = { expenseSelected = true },
                        modifier = Modifier.weight(1f),
                        shape = RoundedCornerShape(topStart = 8.dp, bottomStart = 8.dp)
                    ) {
                        Text(
                            text = "支出",
                            color = if (expenseSelected) Color.White else Color.Gray
                        )
                    }
                    
                    Button(
                        onClick = { expenseSelected = false },
                        modifier = Modifier.weight(1f),
                        shape = RoundedCornerShape(topEnd = 8.dp, bottomEnd = 8.dp)
                    ) {
                        Text(
                            text = "收入",
                            color = if (!expenseSelected) Color.White else Color.Gray
                        )
                    }
                }
                
                // 交易表单
                Card(
                    modifier = Modifier.fillMaxWidth(),
                    shape = RoundedCornerShape(16.dp),
                    colors = CardDefaults.cardColors(containerColor = Color.White.copy(alpha = 0.1f))
                ) {
                    Column(
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(16.dp),
                        verticalArrangement = Arrangement.spacedBy(16.dp)
                    ) {
                        // 金额输入
                        OutlinedTextField(
                            value = uiState.amount,
                            onValueChange = { viewModel.setAmount(it) },
                            label = { Text("金额") },
                            keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
                            modifier = Modifier.fillMaxWidth(),
                            singleLine = true,
                            colors = OutlinedTextFieldDefaults.colors(
                                unfocusedBorderColor = Color.White.copy(alpha = 0.3f),
                                focusedBorderColor = Color.White.copy(alpha = 0.5f)
                            )
                        )
                        
                        if (uiState.amountError) {
                            Text(
                                text = "请输入有效金额",
                                color = Color.Red,
                                fontSize = 12.sp
                            )
                        }
                        
                        // 分类选择
                        if (!uiState.isLoading) {
                            val categories = if (expenseSelected) uiState.expenseCategories else uiState.incomeCategories
                            
                            Text(
                                text = "选择分类",
                                fontWeight = FontWeight.Bold
                            )
                            
                            Row(
                                modifier = Modifier.fillMaxWidth(),
                                horizontalArrangement = Arrangement.spacedBy(8.dp)
                            ) {
                                categories.forEach { category ->
                                    Button(
                                        onClick = { viewModel.setCategory(category.id) },
                                        modifier = Modifier.weight(1f, fill = false),
                                        shape = RoundedCornerShape(8.dp)
                                    ) {
                                        Text(
                                            text = category.name,
                                            fontSize = 12.sp
                                        )
                                    }
                                }
                            }
                            
                            if (uiState.categoryError) {
                                Text(
                                    text = "请选择分类",
                                    color = Color.Red,
                                    fontSize = 12.sp
                                )
                            }
                        }
                        
                        // 备注输入
                        OutlinedTextField(
                            value = uiState.note,
                            onValueChange = { viewModel.setNote(it) },
                            label = { Text("备注") },
                            modifier = Modifier.fillMaxWidth(),
                            colors = OutlinedTextFieldDefaults.colors(
                                unfocusedBorderColor = Color.White.copy(alpha = 0.3f),
                                focusedBorderColor = Color.White.copy(alpha = 0.5f)
                            )
                        )
                    }
                }
                
                // 保存按钮
                Button(
                    onClick = { viewModel.saveTransaction() },
                    modifier = Modifier.fillMaxWidth()
                ) {
                    Text(
                        text = "保存交易",
                        modifier = Modifier.padding(vertical = 8.dp)
                    )
                }
                
                // 错误信息
                if (uiState.generalError != null) {
                    Text(
                        text = uiState.generalError ?: "",
                        color = Color.Red,
                        fontSize = 14.sp
                    )
                }
            }
        }
    }
} 